﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_AddEditProduct : System.Web.UI.Page
{
    private Logging objLogging = new Logging();
    private string strApplicationName = MethodBase.GetCurrentMethod().DeclaringType.Name;
    int _id = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(Request.QueryString.Get("Id")))
            {
                _id = Convert.ToInt32(Request.QueryString.Get("Id"));
            }
            else
            {
                ListView1.Enabled = false;
                ListView1.Visible = false;
            }
            if (!Page.IsPostBack && _id > -1)
            {
                using (ProjectASPEntities myEntities = new ProjectASPEntities())
                {
                    getProduct_Result product = myEntities.getProduct(_id).First();
                    if (product != null)
                    {
                        NameText.Text = product.Name;
                        DescriptionText.Text = product.Description;
                        ProductTypeList.DataBind();
                        ListItem TypeItem = ProductTypeList.Items.FindByValue(product.Type.ToString());
                        if (TypeItem != null)
                        {
                            TypeItem.Selected = true;
                        }
                        BrandList.DataBind();
                        ListItem brandItem = BrandList.Items.FindByValue(product.Brand.ToString());
                        if (brandItem != null)
                        {
                            brandItem.Selected = true;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            objLogging.WriteLine(strApplicationName, ex.Message);
        }
    }
    protected void SaveButton_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                using (ProjectASPEntities myEntities = new ProjectASPEntities())
                {
                    if (_id == -1) // Insert new item
                    {
                        myEntities.insertProduct(Convert.ToInt32(ProductTypeList.SelectedValue),
                            NameText.Text, Convert.ToInt32(BrandList.SelectedValue), DescriptionText.Text);
                    }
                    else // update existing item
                    {
                        myEntities.updateProduct(_id, Convert.ToInt32(ProductTypeList.SelectedValue),
                            NameText.Text, Convert.ToInt32(BrandList.SelectedValue), DescriptionText.Text);
                    }
                    Response.Redirect("Products.aspx");
                }
            }
        }
        catch (Exception ex)
        {
            objLogging.WriteLine(strApplicationName, ex.Message);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/AddPicture.aspx?Id=" + Request.QueryString[0]);
    }
    protected void EntityDataSource1_Inserting(object sender, EntityDataSourceChangingEventArgs e)
    {
        try
        {
            int ProductId = Convert.ToInt32(Request.QueryString.Get("Id"));
            Pictures myPicture = (Pictures)e.Entity;
            myPicture.Product = ProductId;

            FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
            string virtualFolder = "~/Pics/";
            string physicalFolder = Server.MapPath(virtualFolder);
            string fileName = Guid.NewGuid().ToString();
            string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
            FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
            myPicture.ImageUrl = virtualFolder + fileName + extension;
        }
        catch (Exception ex)
        {
            objLogging.WriteLine(strApplicationName, ex.Message);
        }
    }

    protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        try
        {
            FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
            if (!FileUpload1.HasFile || !(FileUpload1.FileName.ToLower().EndsWith(".jpg") || FileUpload1.FileName.ToLower().EndsWith(".jpeg")))
            {
                CustomValidator cusValImage = (CustomValidator)ListView1.InsertItem.FindControl("cusValImage");
                cusValImage.IsValid = false;
                e.Cancel = true;
            }
        }
        catch (Exception ex)
        {
            objLogging.WriteLine(strApplicationName, ex.Message);
        }
    }
}